package com.disease.chronic.Service.Impl;

import com.disease.chronic.Exception.ChronicExceptionEnum;
import com.disease.chronic.Global.Result;
import com.disease.chronic.Mappers.IndicatorsDetailMapper;
import com.disease.chronic.Service.CalendarService;
import com.disease.chronic.dto.CalendarRequestDto;
import com.disease.chronic.dto.CalendarResponseDto;
import com.disease.chronic.pojo.IndicatorsDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class CalendarServiceImpl implements CalendarService {

    @Autowired
    IndicatorsDetailMapper indicatorsDetailMapper;


    public List<CalendarResponseDto> getCurrentCalendarList(Integer year, Integer month){
        List<CalendarResponseDto> currentCalendar = new ArrayList<>();

        //做万年历
        boolean isRun = false;
        if((year%4==0&&year%100!=0) || (year%400==0)) {
            isRun = true;
        }
        //计算从1900-01-01到现在的天数
        int totalDays = 0;
        for(int i=1900;i<year;i++) {
            if((i%4==0&&i%100!=0) || (i%400==0)) {
                totalDays += 366;
            }else {
                totalDays += 365;
            }
        }
        int beforeDays = 0;
        int currentDays = 0;
        for(int j=1;j<=month;j++) {
            switch(j) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    currentDays = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    currentDays = 30;
                    break;
                case 2:
                    if(isRun) {
                        currentDays = 29;
                    }else {
                        currentDays = 28;
                    }
                    break;
            }
            if(j<month) {
                beforeDays += currentDays;
            }
        }
        totalDays += beforeDays;
        int firstDayOfMonth = totalDays%7 + 1;
        if(firstDayOfMonth == 7) {
            firstDayOfMonth = 0;
        }
        for(int k=0;k<firstDayOfMonth;k++) {
            currentCalendar.add(new CalendarResponseDto());
        }
        for(int i=1;i<=currentDays;i++) {
            String m = month<10?"0"+month:month+"";  //   3 03  4   04
            String d = i<10?"0"+i:i+"";
            currentCalendar.add(new CalendarResponseDto(year+"-"+m+"-"+d));
        }
        return currentCalendar;
    }

    public static void main(String[] args) {
        System.out.println(new CalendarServiceImpl().getCurrentCalendarList(2025,4));
    }

    @Override
    public Result getCurrentIndicatorDetail(CalendarRequestDto calendarRequestDto) {
        //获取指定年，月的列表
        String username = calendarRequestDto.getUsername();
        String indicatorName = calendarRequestDto.getIndicatorName();
        Integer year = calendarRequestDto.getYear();
        Integer month = calendarRequestDto.getMonth();
        List<CalendarResponseDto> currentCalendarList = getCurrentCalendarList(year, month);

        for(int i =0 ;i<currentCalendarList.size();i++){
            CalendarResponseDto calendarResponseDto = currentCalendarList.get(i);
            if(calendarResponseDto.getYmd()!=null){
                //按时间、用户名、指标名查找
                IndicatorsDetail indicatorsDetail = indicatorsDetailMapper.selectByUsernameAndIndicatorNameAndDay(username, indicatorName,
                        Date.valueOf(calendarResponseDto.getYmd()));
                //将查找到的指标值存入对应日期数据
                if(indicatorsDetail!=null)
                    calendarResponseDto.setValue(indicatorsDetail.getValue());

            }
        }

        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage(),currentCalendarList);

    }
}

